<?php

namespace App\Api\Eloquent;


class ArticleCategory extends Model
{
    //
    protected $table='article_categories';

    /**
     * 查询该分类下的前$id条文章
     */
    public function get_articles_limit(int $id){
//      var_dump($this->id);
//      dd(Article::all());
        return Article::where('article_category_id',$this->id)
            ->where('recommended',1)
            ->where('display',1)
            ->where('published_at','<=',date('Y-m-d H:i:s'))
            ->orderBy('published_at','desc')
            ->orderBy('sort','desc')
            ->limit($id)
            ->get();
    }

    /**
     * 查询该文章分类下的文章,按指定数量分页显示
     */
    public function get_articles_paginate(int $number=15){
        return Article::where('article_category_id',$this->id)
            ->where('display',1)      //显示
            ->where('published_at','<=',date('Y-m-d H:i:s'))  //到发布时期
            ->orderBy('published_at','desc')  //发布日期降序
            ->orderBy('sort','desc')          //sort降序
            ->paginate($number);
    }

    /**
     * 查询当前分类下所有指定条数的推荐文章
     *
     * @param int $number
     * @return mixed
     */
    public function get_recommend_articles(int $number){
        return Article::where('article_category_id',$this->id)
            ->where('recommended',1) //推荐
            ->where('display',1)      //显示
            ->where('published_at','<=',date('Y-m-d H:i:s'))  //到发布时期
            ->orderBy('published_at','desc')  //发布日期降序
            ->orderBy('sort','desc')          //sort降序
            ->limit($number)
            ->get();
    }
    /**
     * 获取某个文章分类 的子分类列表
     *
     * @param int $article_category_id
     * @return mixed
     */
    public static function get_child_categories(int $article_category_id){
        return ArticleCategory::where('parent',$article_category_id)
//        ->where('recommended','1')
            ->orderBy('sort','desc')
            ->get();
    }

    /**
     * 查询 某个文章分类是否是另一个分类的子级分类
     *
     * @param int $parent
     * @param int $child
     * @return mixed
     */
    public static function is_child_category(int $parent,int $child){
        return ArticleCategory::where('parent',$parent)
            ->where('id',$child)
            ->exists();
    }

    /**
     * 查询某篇文章是否是某个分类下的
     *
     * @param int $article_category_id
     * @param int $article_id
     * @return mixed
     */
    public static function is_child_article(int $article_category_id,int $article_id){
        return Article::where('article_category_id',$article_category_id)
            ->where('id',$article_id)
            ->exists();
    }
}
